<template>
    <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
    import { getGroupIndustryTypeByDept } from '@/api/case/statisticalChart'
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    }
  },
  data() {
    return {
      chart: null,
        xAxisData: [],
        chartData:[]
    }
  },
  mounted() {
      this.getGroupIndustryTypeByDept();
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
      getGroupIndustryTypeByDept(){
          getGroupIndustryTypeByDept().then(
              (res) => {
                  if(res.msg == ''){
                      return;
                  }
                  let data = JSON.parse(res.msg);
                  let obj = {},arr=[];
                  for(let i=0;i<data.length;i++){
                      this.xAxisData.push(data[i].dept_name);
                      obj['arr'+i] = [];
                      arr['arr'+i] = [];
                      for(let j=0;j<data[i].datas.length;j++){
                          obj['arr'+i].push(data[i].datas[j].value);
                          arr['arr'+i].push(data[i].datas[j].name);
                      }
                      this.chartData.push({
                          type: 'bar',
                          stack: 'vistors',
                          barWidth: '150',
                          data:obj['arr'+i] ,
                          animationDuration
                      })
                  }
                  this.$nextTick(() => {
                      this.initChart()
                  })
              }
          ).catch((error) => {
              console.log(error, 'getGroupIndustryTypeByDept');
          })
      },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      this.chart.setOption({
          title: {
              text: '负责监管行业类别案件统计',
              textStyle:{
                  color: '#6b6b6b',
                  fontSize: 15,
                  fontWeight: 'normal'
              },
              left: 'center'
          },
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          top: 30,
          left: '2%',
          right: '2%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [{
          type: 'category',
          data: this.xAxisData,
          axisTick: {
            alignWithLabel: true
          }
        }],
        yAxis: [{
          type: 'value',
          axisTick: {
            show: false
          }
        }],
        series: this.chartData
      })
    }
  }
}
</script>
